﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Crystal.CLL;
using CrystalCommon.Presenter;

namespace CrystalCommon.ControlLib
{
    public partial class Login : BaseForm, ILoginView
    {
        #region Fields
        //public event CheckUserCrendentialsEventHandler CheckUserCrendentials;
        public event CheckUserNameEventHandler CheckUserName;
        private ILoginPresenter LoginPresenter;
        public bool IsCorrectCredentials = false;
        #endregion

        public Login()
        {
            InitializeComponent();
            LoginPresenter = new LoginPresenter(this);
        }

        #region Events
        private void Login_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                UnloadEvents();
            }
            catch (Exception ex)
            {
                this.LogErrorToDatabase(ex);
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (CheckUserName(txtUserName.Text.Trim(), txtPassword.Text.Trim()))
            {
                IsCorrectCredentials = true;
                this.Close();
                
            }
            else
            {
                IsCorrectCredentials = false;
                ShowErrorMessage(DBResult.LOGIN_FAILED, BaseClass.Capitalize("Login"));
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        #endregion

        #region Methods
        private void UnloadEvents()
        {
            //this.CheckUserCrendentials -= this.LoginPresenter.view_CheckUserCredentials;
        }
        #endregion

    }
}
